import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class StudentOperation extends JFrame implements ActionListener
{
	
	
	JButton  btnAddStudent,btnExit;
	JLabel lblHeading,lblStudentName,lblStudentIDNo,lblStudentAge,lblStudentNo,lblStudentAddress,lblStudentTel,lblBatch;
	JTextField txtStudentIDNo,txtStudentName,txtStudentAge,txtStudentNo,txtStudentAddress,txtStudentTel;
	
	JComboBox cmbBatch;
	DefaultComboBoxModel cmbModel;
	
	//Singleton
	private static StudentOperation so=null;
	
	private StudentOperation()
	{
		//Outer Panel
		JPanel screenMain= (JPanel)getContentPane( );
		screenMain.setLayout( new BorderLayout() );	
		screenMain.setBorder(new EmptyBorder(10, 10, 10, 10));		

		//
		//Inner Panel
		JPanel screenSub=new JPanel( );
		screenSub.setLayout( new GridLayout( 7,0,8,8));		
		//
		
		//Labels
		//Heading
		lblHeading=new JLabel(ProgramUitl.ADD_STUDENT_TEXT);
		Font fontHeading=new Font(ProgramUitl.FONT_NAME,
									ProgramUitl.HEADING_FONT_STLYE,
									ProgramUitl.HEADING_FONT_SIZE);
		lblHeading.setFont(fontHeading);
		screenMain.add("North",lblHeading);		
		//
		Font fontLbl=new Font(ProgramUitl.FONT_NAME,
									ProgramUitl.NORMAL_FONT_STLYE,
									ProgramUitl.NORMAL_FONT_SIZE);

		//lblStudentNo=new JLabel(ProgramUitl.STUDNET_NO_TEXT);						
		//lblStudentNo.setFont(fontLbl);
		
		lblStudentIDNo=new JLabel(ProgramUitl.STUDNET_ID_TEXT);						
		lblStudentIDNo.setFont(fontLbl);
		
		lblStudentName=new JLabel(ProgramUitl.STUDNET_NAME_TEXT);						
		lblStudentName.setFont(fontLbl);
		
		lblStudentAge=new JLabel(ProgramUitl.STUDNET_AGE_TEXT);						
		lblStudentAge.setFont(fontLbl);						
		
		lblStudentAddress=new JLabel(ProgramUitl.STUDNET_ADDRESS_TEXT);						
		lblStudentAddress.setFont(fontLbl);
		
		lblStudentTel=new JLabel(ProgramUitl.STUDNET_TP_TEXT);						
		lblStudentTel.setFont(fontLbl);								

		lblBatch=new JLabel(ProgramUitl.STUDNET_BATCH_TEXT);						
		lblBatch.setFont(fontLbl);								
			
		//
		
		//Text Boxes
		//txtStudentNo=new JTextField();
		txtStudentIDNo=new JTextField();
		txtStudentName=new JTextField();
		txtStudentAge=new JTextField();
		txtStudentAddress=new JTextField();
		txtStudentTel=new JTextField();		
		//
		
		//Drop down
		cmbBatch=populateBatchList(cmbBatch);
		//
		
		//Buttons
		btnAddStudent=new JButton(ProgramUitl.ADD_TEXT);
		btnExit=new JButton(ProgramUitl.EXIT_TEXT,ProgramUitl.getButtonIcon(ProgramUitl.ICON_TYPE.ERROR));
		
		btnAddStudent.addActionListener(this);
		btnExit.addActionListener(this);
		//
		
		//Layout Design
		//screenSub.add(lblStudentNo);
		//screenSub.add(txtStudentNo);
		
		screenSub.add(lblStudentIDNo);
		screenSub.add(txtStudentIDNo);
		
		screenSub.add(lblStudentName);
		screenSub.add(txtStudentName);
		
		screenSub.add(lblStudentAge);
		screenSub.add(txtStudentAge);
		
		screenSub.add(lblStudentAddress);
		screenSub.add(txtStudentAddress);

		screenSub.add(lblStudentTel);
		screenSub.add(txtStudentTel);
		
		screenSub.add(lblBatch);
		screenSub.add(cmbBatch);
		
		screenSub.add(btnAddStudent);		
		screenSub.add(btnExit);		
		//
		
		//Inner panel added to Outer Panel 
		screenMain.add("Center",screenSub);
		//
		
		//Frame Settings
		addWindowListener(new WindowListener() {			
			public void windowClosing(WindowEvent e){}
			public void windowOpened(WindowEvent e) 
			{
				MainMenu.getInstance().setVisible(false);
				MainMenu.getInstance().setEnabled(false);
			}			       
			public void windowClosed(WindowEvent e) {}
			public void windowIconified(WindowEvent e) {}
			public void windowDeiconified(WindowEvent e) {}
			public void windowActivated(WindowEvent e) {}
			public void windowDeactivated(WindowEvent e) {}
		});


		
		setSize(ProgramUitl.SMALL_SCREEN_WIDTH,ProgramUitl.SMALL_SCREEN_HEIGHT);
		setLocation(ProgramUitl.SCREEN_LOCATION_TOP,ProgramUitl.SCREEN_LOCATION_LEFT);
		setLocationRelativeTo(null);
		setTitle(ProgramUitl.HEADNING_TEXT);		
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);		
		setUndecorated(true);		
		setVisible(true);
		
		validate();
		//
	}
	
	public static StudentOperation getInstance()
	{
		if(so==null)
		{
			so=new StudentOperation();
		}
		return so;
	}
	//

	//Methods
	boolean Confirmation(String txt)
	{
		int confirmed = JOptionPane.showConfirmDialog(null, 
				txt, ProgramUitl.ADD_STUDENT_TEXT, 
				JOptionPane.YES_NO_OPTION); 
				
		return confirmed == JOptionPane.YES_OPTION;
	}	
	public void add()
	{	
		if(isValidInputs())
		{
			if(Confirmation(ProgramUitl.WISH_TO_PROCEED))
			{
				Student s=new Student();
				
				s.setNo(XmlOperations.getInstance().getStudentNo());
				s.setIdNo(ProgramUitl.senatizeData(txtStudentIDNo.getText().trim()));
				s.setName(ProgramUitl.senatizeData(txtStudentName.getText().trim()));
				s.setAge(Integer.parseInt(txtStudentAge.getText().trim()));
				s.setAddress(txtStudentAddress.getText().trim());
				s.setTel(ProgramUitl.senatizeData(txtStudentTel.getText().trim()));
				s.setBatchCode(ProgramUitl.getBatchNoFromListItem(String.valueOf(cmbBatch.getSelectedItem())));
				s.setStatus(ProgramUitl.STATUS.ENABLED);
				
				try
				{
					ProgramUitl.ERROR_TYPE rt=XmlOperations.getInstance().addStudent(s);
					XmlOperations.getInstance().enrollStudentsByBatch(Integer.toString(s.getBatchCode()));
					
					switch(rt)
					{
						case ADDED:
							{					
								JOptionPane.showMessageDialog(null,s.getName() + ProgramUitl.ADDED_SUCCESS_TEXT,ProgramUitl.ADD_STUDENT_TEXT,JOptionPane.INFORMATION_MESSAGE);
								clearText();						
							}
							break;
						case ALREADY_EXISTS:
							JOptionPane.showMessageDialog(null,txtStudentIDNo.getText().trim() + ProgramUitl.ALREADY_EXISTS_TEXT,ProgramUitl.ADD_STUDENT_TEXT,JOptionPane.WARNING_MESSAGE);
							break;
						default:
							break;
					}
					s=null;
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null,ex.getMessage(),ProgramUitl.ADD_STUDENT_TEXT,JOptionPane.ERROR_MESSAGE);	
				}
			}
		}
	}
		
	void exit() 
	{ 		
		int confirmed = JOptionPane.showConfirmDialog(null, 
				ProgramUitl.EXIT_PRORAM_MSG, ProgramUitl.ADD_STUDENT_TEXT, 
				JOptionPane.YES_NO_OPTION); 
				
		if (confirmed == JOptionPane.YES_OPTION) 
		{        
				MainMenu.getInstance().setEnabled(true);
				MainMenu.getInstance().setVisible(true);
				so=null;
				this.dispose();
				
		} 
	}
	
	public boolean isValidInputs()
	{
		String msg="";
		
		//NIC
		if(ProgramUitl.IsEmptyString(txtStudentIDNo.getText().trim()))
		{
			msg+=ProgramUitl.buildMessageItem(lblStudentIDNo.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}
		else if(ProgramUitl.validateNic(txtStudentIDNo.getText().trim())==false)
		{
			msg+=ProgramUitl.buildMessageItem(lblStudentIDNo.getText().trim() + ProgramUitl.INVALID_FORMAT_ERROR);
		}
		
		//Name
		if(ProgramUitl.IsEmptyString(txtStudentName.getText().trim()))
		{
			msg+=ProgramUitl.buildMessageItem(lblStudentName.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}
		
		//Age
		if(ProgramUitl.IsEmptyString(txtStudentAge.getText().trim()))
		{
			msg+=ProgramUitl.buildMessageItem(lblStudentAge.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}
		else if(ProgramUitl.IsCorrectAget(txtStudentAge.getText().trim())==false)
		{
			msg+=ProgramUitl.buildMessageItem(lblStudentAge.getText().trim() + ProgramUitl.INVALID_FORMAT_ERROR);
		}
		
		//Address
		if(ProgramUitl.IsEmptyString(txtStudentAddress.getText().trim()))
		{
			msg+=ProgramUitl.buildMessageItem(lblStudentAddress.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}
		
		//Tel
		if(ProgramUitl.IsEmptyString(txtStudentTel.getText().trim()))
		{
			msg+=ProgramUitl.buildMessageItem(lblStudentTel.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}	
		else if(ProgramUitl.isPhoneNumberValid(txtStudentTel.getText().trim())==false)
		{
			msg+=ProgramUitl.buildMessageItem(lblStudentTel.getText().trim() + ProgramUitl.INVALID_FORMAT_ERROR);
		}

		//Batch		
		if(cmbBatch.getSelectedItem()==null)
		{
			msg+=ProgramUitl.buildMessageItem(lblBatch.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}	
						
		if(ProgramUitl.IsEmptyString(msg)!=true)
		{
			JOptionPane.showMessageDialog(null,msg,ProgramUitl.ADD_STUDENT_TEXT,JOptionPane.ERROR_MESSAGE);
		}
		return ProgramUitl.IsEmptyString(msg);
	}
	
	void clearText()
	{		
		txtStudentIDNo.setText(null);
		txtStudentName.setText(null);
		txtStudentAge.setText(null);
		txtStudentAddress.setText(null);
		txtStudentTel.setText(null);
		txtStudentIDNo.requestFocus();
	}
	
	JComboBox populateBatchList(JComboBox cmbBox)
	{
		java.util.List<Batch> b=XmlOperations.getInstance().getAllBatches();
		
		Vector cmbItems=new Vector();
		
		if(b==null)
		{
			cmbBox=new JComboBox();
		}
		else
		{
			
			cmbModel = new DefaultComboBoxModel(cmbItems);
			for (int i = 0; i < b.size(); i++) 
			{
				Batch bTmp=(Batch)b.get(i);
				cmbItems.add(ProgramUitl.buildBatchListItem(bTmp));
			}
			cmbBox = new JComboBox(cmbModel);
		}
		return cmbBox;
	}
	//
	
	//Events
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource().equals(btnExit))
		{
			exit();
		}	
		else if(ae.getSource().equals(btnAddStudent))
		{			
			add();			
		}		
	}	
	//	
}